package pers.flowerinsnow.minecraft.forge.fastdungeonjoiner
import net.minecraft.client.Minecraft
import net.minecraft.util.{ChatComponentText, IChatComponent}
import net.minecraftforge.client.event.ClientChatReceivedEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.Mod.EventHandler
import net.minecraftforge.fml.common.event.{FMLInitializationEvent, FMLPostInitializationEvent, FMLPreInitializationEvent}
import org.apache.logging.log4j.Logger
import pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.listener.KeyListener
import pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.manager.{ConfigManager, KeyManager}

@Mod(
  modid = FastDungeonJoiner.MODID,
  name = "FastDungeonJoiner",
  version = FastDungeonJoiner.VERSION,
  clientSideOnly = true,
  acceptedMinecraftVersions = "[1.7,)"
)
object FastDungeonJoiner {
  final val MODID = "fastdungeonjoiner"
  final val VERSION = "*(VERSION)"
  final var logger : Logger = _

  def addChatComponentMessage(chat : IChatComponent): Unit = {
    val event : ClientChatReceivedEvent = new ClientChatReceivedEvent(0.byteValue(), chat)
    MinecraftForge.EVENT_BUS.post(event)
    if (!event.isCanceled)
      Minecraft.getMinecraft.thePlayer.addChatComponentMessage(event.message)
  }
  def addChatComponentMessage(chat : String): Unit = {
    Minecraft.getMinecraft.thePlayer.addChatComponentMessage(new ChatComponentText(chat))
  }
}

@Mod(
  modid = FastDungeonJoiner.MODID,
  name = "FastDungeonJoiner",
  version = FastDungeonJoiner.VERSION,
  clientSideOnly = true,
  acceptedMinecraftVersions = "[1.7,)"
)
class FastDungeonJoiner {
  @EventHandler
  def preInit(event : FMLPreInitializationEvent): Unit = {
    FastDungeonJoiner.logger = event.getModLog
    ConfigManager.init(event.getSuggestedConfigurationFile)
  }
  @EventHandler
  def init(event: FMLInitializationEvent): Unit = {
    KeyManager.register()
    registerListeners(new KeyListener)
  }
  @EventHandler
  def postInit(event : FMLPostInitializationEvent): Unit = {

  }

  private def registerListeners(listeners : Any*): Unit = {
    listeners.foreach(l => MinecraftForge.EVENT_BUS.register(l))
  }
}
